import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/Manager.vue'
const originalPush = VueRouter.prototype.push

//解决导航栏或底部导航taBar中的vue-router在3.0版本以上频繁点击菜单报错的问题
VueRouter.prototype.push = function push(location) {

    return originalPush.call(this, location).catch(err => err)

}

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'Manager',
        component: () => import('../views/Manager.vue'),
        redirect:'/home',
        children:[

            {
                path:'/home',
                name:'Home',
                meta:{name:'系统首页'},
                component: () => import('../views/manager/Home.vue')
            },
            {
                path:'/photo',
                name:'Photo',
                meta:{name:'我的相冊'},
                component: () => import('../views/manager/Photo.vue')
            },
            {
                path:'/album',
                name:'Album',
                meta:{name:'相册'},
                component: () => import('../views/manager/Album.vue')
            },
            {
                path:'/favorite',
                name:'Favorite',
                meta:{name:'收藏笔记'},
                component: () => import('../views/manager/Favorite.vue')
            },
            {
                path: '/user',
                name: 'User',
                meta:{name:'信息管理-用户信息'},
                component: () => import('../views/manager/User.vue')
            },
            {
                path: '/admin',
                name: 'Admin',
                meta:{name:'信息管理-管理员信息'},
                component: () => import('../views/manager/Admin.vue')
            },
            {
                path: '/403',
                name: '403',
                meta: { requiresAuth: true },
                component: () => import('../views/manager/403.vue')
            },
            {
                path: '/person',
                name: 'Person',
                meta:{name:'个人信息'},
                component: () => import('../views/manager/Person.vue')
            },
            {
                path: '/password',
                name: 'Password',
                meta:{name:'修改密码'},
                component: () => import('../views/manager/Password.vue')
            },

            {
                path: '/newsDetail',
                name: 'NewsDetail',
                meta:{name:'新闻详情页'},
                component: () => import('../views/manager/NewsDetail.vue')
            },
            {
                path: '/notice',
                name: 'Notice',
                meta:{name:'系统公告'},
                component: () => import('../views/manager/Notice.vue')
            }


        ]
    },
    {
        path: '/news',
        name: 'News',
        meta:{name:'新闻信息'},
        component: () => import('../views/manager/News.vue')
    },
    {
        path: '/manager3',
        name: 'Manager3',
        component: () => import('../views/Manager3.vue'),
        redirect: '/noteList',
        children: [
            {
                path:'/noteList',
                name:'noteList',
                component: () => import('../views/manager/NoteList.vue')
            },
            {
                path:'/note',
                name:'note',
                component: () => import('../views/manager/Note.vue')
            }
        ]
    },

    {
        path: '/element',
        name: 'Element',
        meta: { requiresAuth: true },
        component: () => import('../views/Element.vue')
    },

    {
        path: '/login',
        name: 'login',
        meta: { requiresAuth: true },
        component: () => import('../views/Login.vue')
    },
    {
        path: '/loginByPhone',
        name: 'loginByPhone',
        meta: { requiresAuth: true },
        component: () => import('../views/LoginByPhone.vue')
    },
    {
        path: '/register',
        name: 'Register',
        meta: { requiresAuth: true },
        component: () => import('../views/Register.vue')
    },
    {
        path: '*',
        name: '404',
        meta: { requiresAuth: true },
        component: () => import('../views/404.vue')
    }


]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
//路由守卫
router.beforeEach(( to, from, next)=>{
    let isAuthenticated = false;
    if(localStorage.getItem('honey-user')!==null){
        isAuthenticated = true;
    }
    /* 判断用户是否已登录的逻辑 */;
    const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
    if (!requiresAuth && !isAuthenticated) {
        next({ name: 'login' }); // 如果需要认证且未登录，则跳转到登录页面
    } else {
        next(); // 否则继续导航
    }
    // //to 到达的路由信息
    // //from是开源的路由信息s
    // //next是帮助我们跳转路由的函数
    // let adminPath = ['/user']
    // let user = JSON.parse(localStorage.getItem('honey-user')||'{}')
    //
    // //如果当前登录的用户不是管理员，然后当前到达的路径是管理员才有权限访问的路径，那么就让用户去到一个没有权限的页面，不让他访问
    // if (user.role!=='管理员' && adminPath.includes(to.path)){
    //         next('/403')
    // }else {
    //     next()
    // }

})

export default router
